package com.zbkj.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.zbkj.dal.entity.product.Parameter;
import com.zbkj.dal.entity.product.ProductTemplateParameter;
import com.zbkj.dal.pojo.response.ba.ProductParameterCombineResponse;
import com.zbkj.dal.dao.ParameterMapper;
import com.zbkj.dal.dao.ProductTemplateParameterMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.service.service.ProductTemplateParameterService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 模板参数 服务实现类
 * </p>
 *
 * @author jhui
 * @since 2023-10-31
 */
@Service
public class ProductTemplateParameterServiceImpl extends ServiceImpl<ProductTemplateParameterMapper, ProductTemplateParameter> implements ProductTemplateParameterService {

    @Autowired
    private ParameterMapper parameterMapper;

    @Override
    public void deleteByTemplateId(Integer templateId) {
        LambdaQueryWrapper<ProductTemplateParameter> queryWrapper = Wrappers.lambdaQuery(ProductTemplateParameter.class);
        queryWrapper.eq(ProductTemplateParameter::getProductTemplateId, templateId);
        remove(queryWrapper);
    }

    @Override
    public List<ProductTemplateParameter> listByTemplateId(Integer templateId) {
        LambdaQueryWrapper<ProductTemplateParameter> queryWrapper = Wrappers.lambdaQuery(ProductTemplateParameter.class);
        queryWrapper.eq(ProductTemplateParameter::getProductTemplateId, templateId);
        return list(queryWrapper);
    }

    @Override
    public List<ProductParameterCombineResponse> listParameterByTemplateId(Integer templateId) {
        LambdaQueryWrapper<ProductTemplateParameter> queryWrapper = Wrappers.lambdaQuery(ProductTemplateParameter.class);
        queryWrapper.eq(ProductTemplateParameter::getProductTemplateId, templateId);
        List<ProductTemplateParameter> list = list(queryWrapper);
        List<ProductParameterCombineResponse> productParameterCombineResponses = Lists.newArrayList();
        if (CollectionUtils.isEmpty(list)) {
            return productParameterCombineResponses;
        }
        List<Integer> paramIds = list.stream().map(m -> m.getParameterId()).distinct().collect(Collectors.toList());
        List<Parameter> parameters = parameterMapper.selectBatchIds(paramIds);
        for (Parameter parameter : parameters) {
            ProductParameterCombineResponse productParameterCombineResponse = ProductParameterCombineResponse.create(parameter);
            productParameterCombineResponses.add(productParameterCombineResponse);
        }
        return productParameterCombineResponses;
    }
}
